const formatTime = val => {
  let value = val;
  if (typeof val == "string") {
    value = val.replace(/\-/g, "/");
    value = value.replace(/T/g, " ");
    value = value.split(".")[0];
  }

  const date = new Date(value);
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();

  return (
    [year, month, day].map(formatNumber).join("-") +
    " " +
    [hour, minute, second].map(formatNumber).join(":")
  );
};

const formatTimeToDate = val => {
  let value = val;
  if (typeof val == "string") {
    value = val.replace(/\-/g, "/");
    value = value.replace(/T/g, " ");
    value = value.split(".")[0];
  }
  const date = new Date(value);
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();

  return [year, month, day].map(formatNumber).join("-");
};

const formatNumber = n => {
  n = n.toString();
  return n[1] ? n : "0" + n;
};
const promisic = function(func) {
  return function(params = {}) {
    return new Promise((resolve, reject) => {
      const args = Object.assign(params, {
        success: res => {
          resolve(res);
        },
        fail: error => {
          reject(error);
        }
      });
      func(args);
    });
  };
};

const getDistance = function(lat1, lng1, lat2, lng2) {
  lat1 = lat1 || 0;

  lng1 = lng1 || 0;

  lat2 = lat2 || 0;

  lng2 = lng2 || 0;

  var rad1 = (lat1 * Math.PI) / 180.0;

  var rad2 = (lat2 * Math.PI) / 180.0;

  var a = rad1 - rad2;

  var b = (lng1 * Math.PI) / 180.0 - (lng2 * Math.PI) / 180.0;

  var r = 6378137;

  return (
    r *
    2 *
    Math.asin(
      Math.sqrt(
        Math.pow(Math.sin(a / 2), 2) +
          Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(b / 2), 2)
      )
    )
  ).toFixed(0);
};

module.exports = {
  formatTimeToDate: formatTimeToDate,
  formatTime: formatTime,
  promisic: promisic,
  getDistance: getDistance
};
